<%@page import="java.util.ArrayList"%>
<%@page import="rmi.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<jsp:useBean id="manager" scope="session" class="manager.Manager" />
<jsp:useBean id="methodString" scope="request" class="bean.MethodString" />
<%
boolean err = false;
String id = "";
String pwd = "";
String errId = "";
String errPwd = "";
String errConnexion = "";

/*******************************************************
* Vérification des variables passées par le formulaire
*********************************************************/
if(request.getParameter("submit") != null){ 
	id = request.getParameter("ident");
	if(id == null) id = "";
	if(id.trim().equals("")){
		errId = "Veuillez remplir le champ Identifiant";
		err = true;
	}
	pwd = request.getParameter("mdp");
	if(pwd == null) pwd = "";
	if(pwd.trim().equals("")){
		errPwd = "Veuillez remplir le champ Mot de passe";
		err = true;
	}
	/* Si aucune erreure  on analyse la connexion*/
	if(!err){
		/**************************************************************
		* on vérifie la connexion au serveur
		* on encrypte le mot de passe pour cela
		* si la connexion n'est pas valide, on renvoie le client sur le formulaire en lui précisant un message d'erreur
		****************************************************************/
		if(manager.connect(id, methodString.getEncodedPassword(pwd))){
			ArrayList<String> messages = new ArrayList<String>();
			messages.add("Connexion effectuée");
			manager.setMessages(messages);
			response.sendRedirect("/cabinet_medical/accueil/accueil.jsp");
			return;
		}else{
			errConnexion = "Echec de la connexion";
		}
	}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Page de connexion</title>
</head>
<body style="margin-left:25%;font-family:Arial;">
<div id="contenu"  style="width:600px;height:500px;background:url('../images/design/identification.png') no-repeat;">
<div id="" style="width:100%;height:235px;padding:0;padding-left:100px;">
	<!-- Affichage des messages d'erreurs -->
	<table>
		<tr style="height:200px;">
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td style="text-align:center;">
<%
				ArrayList<String> messages = manager.extraireMessages();
				if(messages.size() > 0){
					out.println("<ul style='font-size:12px;list-style-type: none;color:red;'>");
					for(String mess : messages){
						out.println("<li>"+mess+"</li>");
					}
					out.println("</ul>");
				}
%>
			</td>
		</tr>	
	</table>
</div>
<!-- Formulaire de connexion -->
<form action="connexion.jsp" method="post">
	<table style="width:400px;margin:0;margin-left:100px;">
		<tr>
			<td style="font-size:13px;font-weight:bold;width:200px;">Identifiant * :</td>
			<td><input type="text" name="ident" value="<%= id %>" maxlength="255"/></td>
			<td>&nbsp;</td>
		</tr>
<%
		if(!errId.trim().equals("")) {
%>		
			<tr>
				<td>&nbsp;</td>
				<td style="font-size:12px;color:red;font-style:italic;"><%= errId %></td>
				<td>&nbsp;</td>
			</tr>
<%
		}
%>		
		<tr>
			<td style="font-size:13px;font-weight:bold;">Mot de passe * :</td>
			<td><input type="password" name="mdp" value="" maxlength="8"/></td>
			<td>&nbsp;</td>
		</tr>
<%
		if(!errPwd.trim().equals("")) {
%>		
			<tr>
				<td>&nbsp;</td>
				<td style="font-size:12px;color:red;font-style:italic;"><%= errPwd %></td>
				<td>&nbsp;</td>
			</tr>
<%
		}
%>		
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td></td>
			<td style="font-size:12px;color:red;font-style:italic;"><%= errConnexion %></td>
			<td style="text-align:right;"><input type="submit" name="submit" value="Se connecter" /></td>
		</tr>
		<tr> 
			<td style="font-style:italic;font-size:10px;">* : champ obligatoire</td> 
			<td>&nbsp;</td> 
			<td>&nbsp;</td>
		</tr>
	</table>
</form>
</div>
</body>
</html>